package com.base.cn.platform.os.controller.web.qa.record;

import com.base.cn.platform.os.common.WebBaseController;
import com.base.cn.platform.os.service.course.qa.record.QaGainAttendRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PC端我的收益Controller
 *
 * @author LIZHIPENG
 * @create 2018-05-29 下午 6:48
 */
@Controller
@RequestMapping("/web/uc/qa/record")
public class WebQaEarningController extends WebBaseController {

    @Autowired
    private QaGainAttendRecordService qaGainAttendRecordService;

    /**
     * 初始化我的收益页面
     * @param model
     * @return String
     */
    @RequestMapping("/toMyEarningView")
    public String toMyEarningView(Model model){
        return resultPage("/qa/record/qa-my-earning-list");
    }

    /**
     * 查询我的累计收益
     * @param model Model
     * @param request HttpServletRequest
     * @return String
     */
    @RequestMapping("/queryMyEarning")
    public String queryMyEarning(Model model, HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> params = new HashMap<>();
        params.put("receiptorId", userId);
        params.put("userType",3);
        params.put("status",1);
        double sumAmount = qaGainAttendRecordService.getUserQaEarning(params);
        model.addAttribute("sumAmount", sumAmount);
        return resultPage("/qa/record/qa-question-earning");
    }

    /**
     * ajax查询用户的收益详情列表
     * @param model Model
     * @param request HttpServletRequest
     * @param currentPage 页码
     * @param params 查询条件
     * @return String页面
     */
    @RequestMapping("/ajaxQueryMyEarningList")
    public String ajaxQueryMyEarningList(Model model, HttpServletRequest request,
                                         @RequestParam(value = "currentPage", required = false, defaultValue = "1") int currentPage,
                                         @RequestBody Map<String, Object> params){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        params.put("receiptorId", userId);
        params.put("userType",3);
        Map<String, Object> dataMap = qaGainAttendRecordService.queryGainAttendRecordListPage(currentPage, params, false, true);
        model.addAttribute("dataMap", dataMap);
        List<Map<String, Object>> list = (List<Map<String, Object>>)dataMap.get("list");
        return resultPage("/qa/record/ajax-qa-my-earning");
    }

    /**
     * 问答收益提取到我的账户
     * @param request HttpServletRequest
     * @return Map<String, Object>
     */
    @RequestMapping("/extractEarning")
    @ResponseBody
    public Map<String, Object> extractEarning(HttpServletRequest request){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        Map<String, Object> map = new HashMap<>();
        map.put("receiptorId", userId);
        map.put("userType", 3);
        return qaGainAttendRecordService.extractEarning(map);
    }
}
